package pl.edu.pw.mini.java2.Communicator.Client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 * Klasa odpowiedzialna za tworzenie i obsługę GUI do logowania.
 * 
 * @author Dymitr Załunin, Kamil Łysik
 */
public class LoginDialog extends JDialog {

	private static final long serialVersionUID = 6693378945327711350L;
	private Client client;

	/**
	 * Konstruktor bezparametrowy, tworzacy nową instancję obiektu LoginDialog.
	 */
	public LoginDialog() {
		setTitle("Login");
		initUI();
	}

	/**
	 * Konstruktor, tworzacy nową instancję obiektu LoginDialog.
	 * @param client Obiekt klienta. 
	 */
	public LoginDialog(Client client) {
		setTitle("Login");
		setClient(client);
		initUI();
	}

	/**
	 * Ustawia nowego klienta.
	 * @param client Obiekt klienta.  
	 */
	public void setClient(Client client) {
		this.client = client;
	}

	private void initUI() {
		final JTextField loginInput = new JTextField();
		final JTextField ipServerAddrInput = new JTextField();
		final JTextField portServerInput = new JTextField();
		final JLabel login = new JLabel("Login");
		final JLabel ipServerAddr = new JLabel("IP Server:");
		final JLabel portServer = new JLabel("Port Server:");
		JButton okButton = new JButton("Login");
		okButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				login(loginInput.getText(), ipServerAddrInput.getText(),
						portServerInput.getText());
			}
		});
		loginInput.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				login(loginInput.getText(), ipServerAddrInput.getText(),
						portServerInput.getText());
			}
		});
		ipServerAddrInput.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				login(loginInput.getText(), ipServerAddrInput.getText(),
						portServerInput.getText());
			}
		});
		portServerInput.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				login(loginInput.getText(), ipServerAddrInput.getText(),
						portServerInput.getText());
			}
		});
		Box b = Box.createVerticalBox();
		b.add(login);
		b.add(loginInput);
		b.add(ipServerAddr);
		b.add(ipServerAddrInput);
		b.add(portServer);
		b.add(portServerInput);
		b.add(okButton);
		add(b);
		setSize(200, 320);
	}

	private void login(String name, String ip, String port) {
		if (client == null)
			return;
		try {
			client.ConnectToServer(ip, Integer.parseInt(port));
			client.start();
		} catch (Exception ex) {
			JOptionPane.showMessageDialog(this,
					"Nie można połączyć się z serwerem! " + name + "\n" + ip
							+ "\n" + port, "TITLE", JOptionPane.ERROR_MESSAGE);
			System.exit(-1);
		}
		client.setLoginInfo(new LoginInfo(name));
		try {
			client.LogOnRequest();
			setVisible(false);
		} catch (Exception e) {
			client.setLoginInfo(null);
			JOptionPane.showMessageDialog(this, "Login failed", "Error",
					JOptionPane.ERROR_MESSAGE);
		}
	}
}
